<?php
/**
 * 
 * 评论独立  这里不涉及用户或者新闻关联操作
 * @author wolf
 *
 */
class CommentController extends NodeController {
	
	/**
	 * 评论 增加多字段处理 支持跳转位置
	 */
	public function comment() {
		// 查看是否$_GET和$_POST混合提交
		if (! empty ( $_GET )) {
			echo "禁止混合提交，感谢囧NC的支持!";
			exit ();
		}
		
		// 查看该ip一天提交了几次
		if (isset ( $_POST )) {
			$lastInsertId = self::getCommentService ()->add ( $_POST );
			
			if ($lastInsertId > 0) {
				$this->sendNotice ( "提交成功", null, true );
			} else {
				$this->sendNotice ( "提交失败", null, false );
			
			}
		}
	}
	
	public function remove() {
		$id = $_POST ['id'];
		self::getCommentService ()->removeComment ( $id );
		self::getCommentService ()->removeReplyById ( $id );
		//并且删除该下面的所有回复
		$this->sendNotice ( "del success", null, true );
	}
	
	/**
	 * 回复
	 */
	public function reply() {
		// 查看是否$_GET和$_POST混合提交
		if (! empty ( $_GET ) || ! isset ( $_POST )) {
			echo "禁止混合提交，感谢囧NC的支持!";
			exit ();
		}
		
		// 查看该ip一天提交了几次
		if ($_POST ['comment_id'] < 0) {
			$this->sendNotice ( "没有指定回复id", null, false );
		
		}
		
		$lastInsertId = self::getCommentService ()->reply ( $_POST );
		
		if ($lastInsertId > 0) {
			$this->sendNotice ( "提交成功", null, true );
		} else {
			$this->sendNotice ( "提交失败", null, false );
		
		}
	
	}
	
	/**
	 * 删除评论
	 */
	public function cStatus() {
		if (! isset ( $_POST ) || empty ( $_POST )) {
			$this->sendNotice ( "你没有选择任何信息", null, false );
		}
		
		self::getCommentService ()->save ( $_POST ['ids'], $_POST ['type'] );
		echo json_encode ( array ('status' => true, 'message' => '操作成功!' ) );
		$this->sendNotice ( "success", null, true );
	}
	
	/**
	 * 评论搜索
	 * Enter description here ...
	 */
	public function search() {
		
		$type = $_REQUEST ['type'];
		$value = trim ( $_REQUEST ['value'] );
		$where = array ($type => $value );
		$list = self::getCommentService ()->l ( - 1, 1, $where );
		$this->view ()->assign ( 'num', $list ['page'] );
		$this->view ()->assign ( 'totalnum', $list ['totalnum'] );
		$this->view ()->assign ( 'news', $list ['list'] );
		// 在线导出功能
		// 增加指定模板
		

		$cate = require './config/base.php'; // 引入常用分类
		$this->view ()->assign ( 'cate', $cate ['comment'] );
		$this->view ()->display ( 'file:news/clist.tpl' );
	}
	
	/**
	 * 用户列表页 后台显示
	 * @前后短支持 前端为clist.tpl 固定模板 和search.tpl类似
	 */
	public function clist() {
		$cid = isset ( $_GET ['cid'] ) ? $_GET ['cid'] : 0;
		$p = isset ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		
		// 2013-10-15 新增加根据不同的内容id 对应不同的模板 相同的肯定大于不同的原则  文件型模板
		// 默认设置为15条
		if (isset ( $_GET ['num'] )) {
			$num = trim ( $_GET ['num'] );
		} else {
			$newNum = require 'config/base.php';
			$num = $this->_num;
		}
		// $totalNum = 2000;
		$list = self::getCommentService ()->l ( $cid, $p, NULL );
		$this->view ()->assign ( 'nid', $cid );
		$this->view ()->assign ( 'num', $list ['page'] );
		$this->view ()->assign ( 'totalnum', $list ['totalnum'] );
		$this->view ()->assign ( 'news', $list ['list'] );
		// 在线导出功能
		// 增加指定模板
		$cate = require './config/base.php'; // 引入常用分类
		$this->view ()->assign ( 'cate', $cate ['comment'] );
		$this->view ()->display ( "file:news/clist.tpl", $cid );
	}
	
	public static function getCommentService() {
		return new CommentService ();
	}

}